
const yz = require('../comm/yz');


exports.getClass = function (classId) {
    // body...
    var classObj = null
    var typeArray = this.getTypeArray();

    typeArray.forEach(function (type) {

        type.classArray.forEach(function (classItem) {


            if (classItem.classId == classId) {

                classObj = classItem
                return false
            }
        }, this);

    }, this);

    return classObj;

};
exports.getClassName = function (classId) {
    // body...
    var classItme = this.getClass(classId)
    if (classItme) {
        return classItme.className
    }
    else {
        return ""
    }




};







exports.getTypeArray = function (params) {

    return [



        {
            typeName: "成人尺码 s,m,l....",
            typeId: "10",
            classArray: [

                {

                    className: "男装",
                    classId: "nanzhuang"
                },
                {

                    className: "女装",
                    classId: "nvzhuang"
                },
                {

                    className: "成人中性",
                    classId: "chengrenzhongxing"
                }


            ]



        },
        {
            typeName: "儿童尺码 50,65,75....",
            typeId: "100",
            classArray: [

                {

                    className: "男童",
                    classId: "nantong"
                },
                {

                    className: "女童",
                    classId: "nvtong"
                },
                {

                    className: "儿童中性",
                    classId: "ertongzhongxing"
                }


            ]



        }



    ];

}

exports.unitArray = function () {
    // body...
    return [
        {
            id: "100",
            name: "件"
        },
        {
            id: "200",
            name: "套"
        },
        {
            id: "300",
            name: "条"
        }
    ]
};

exports.getUnitName = function (unitId) {
    // body...
    var unitName = ""
    this.unitArray().forEach(function (unit) {

        if (unitId == unit.id) {
            unitName = unit.name
        }

    }, this);
    return unitName


};

//全部尺码字典
function sizeArray(params) {


    var sizeArray = [
        {
            typeId: "10",
            name: "XS",
            id: '5'
        },
        {
            typeId: "10",
            name: "S",
            id: '10'
        },
        {
            typeId: "10",
            name: "M",
            id: '20'
        },
        {
            typeId: "10",
            name: "L",
            id: '30'
        },
        {
            typeId: "10",
            name: "XL",
            id: '40'
        },
        {
            typeId: "10",
            name: "XXL",
            id: '50'
        },
        {
            typeId: "10",
            name: "XXXL",
            id: "60"
        },
        {
            typeId: "100",
            name: "50",
            id: "110",
            bg: 1
        },
        {
            typeId: "100",
            name: "65",
            id: '115'
        },
        {
            typeId: "100",
            name: "75",
            id: '120'
        },
        {
            typeId: "100",
            name: "80",
            id: '125'
        },
        {
            typeId: "100",
            name: "90",
            id: "130"
        },
        {
            typeId: "100",
            name: "100",
            id: "135"
        },
        {
            typeId: "100",
            name: "105",
            id: "138"
        },
        {
            typeId: "100",
            name: "110",
            id: "140"
        },
        {
            typeId: "100",
            name: "115",
            id: "143"
        },
        {
            typeId: "100",
            name: "120",
            id: "145"
        },
        {
            typeId: "100",
            name: "125",
            id: "148"
        },
        {
            typeId: "100",
            name: "130",
            id: "150"
        },
        {
            typeId: "100",
            name: "135",
            id: "153"
        },
        {
            typeId: "100",
            name: "140",
            id: "155"
        },
        {
            typeId: "100",
            name: "145",
            id: "158"
        },
        {
            typeId: "100",
            name: "150",
            id: "160"
        },
        {
            typeId: "100",
            name: "155",
            id: "165"
        },
        {
            typeId: "100",
            name: "160",
            id: "170"
        },
        {
            typeId: "100",
            name: "165",
            id: "175"
        }


    ]
    if (params) {

        var array = [];
        sizeArray.forEach(function (sizeObj) {
            params.forEach(function (sizeId) {

                if (sizeId == sizeObj.id) {

                    array.push(sizeObj)
                }

            }, this);

        }, this);
        return array;
    }
    else {
        return sizeArray;
    }

}


exports.fmtSizeIdArray = function (sizeIdArray) {
    var list = [];
    this.sizeArray().forEach(function (size) {

        sizeIdArray.forEach(function (sizeId) {
            if (size.id == sizeId) {
                list.push(sizeId)
            }
        }, this);
    }, this);
    // body...

    return list
};

exports.sizeArray = sizeArray;

exports.getSize = function (sizeId) {
    // body...
    var size = null
    this.sizeArray().forEach(function (element) {
        if (sizeId == element.id) {
            size = element
        }
    }, this);
    return size
};

exports.getSizeName = function (sizeId) {
    var size = this.getSize(sizeId)

    if (size) {

        return size.name
    }
    else {
        return ""
    }
};




exports.formatData = function (dataItem) {
    //customerOrder.proof  or  bulk
    dataItem = yz.IsNullThen(dataItem, {});
    dataItem.memo = yz.IsNullThen(dataItem.memo, "")
    dataItem.expTime = new Date(yz.IsNullThen(dataItem.expTime, new Date()));
    var sizeIdArray = [];
    var data = [];
    dataItem.sizeIdArray = yz.IsNullThen(dataItem.sizeIdArray, []);
    dataItem.data = yz.IsNullThen(dataItem.data, []);
    dataItem.defaultSizeId = yz.IsNullThen(dataItem.defaultSizeId, "")   //基础码

    // console.log(JSON.stringify(j));
    this.sizeArray().forEach(function (sizeObj) {  //首先给sizeIdArray排序
        dataItem.sizeIdArray.forEach(function (sizeId) {

            if (sizeId == sizeObj.id) {

                sizeIdArray.push(sizeId)
            }
        }, this);
    }, this);




    dataItem.data.forEach(function (color) {


        var newColor = yz.clone(color);
        newColor.sizeArray = [];
        sizeIdArray.forEach(function (sizeId) {

            var sizeItem = {
                id: sizeId,
                num: 0

            };

            color.sizeArray.forEach(function (oldSizeItem) {

                if (oldSizeItem.id == sizeItem.id) {

                    sizeItem = oldSizeItem
                }
            }, this);
            newColor.sizeArray.push(sizeItem)

        }, this);


        data.push(newColor)
    }, this);
    dataItem.sizeIdArray = sizeIdArray
    dataItem.data = data


    return dataItem;

}